<?php
/**
 * Setting_akun Class
 */
class Produksi_rph extends CI_Controller {
	/**
	 * Constructor, load Semester_model, Cabang_model
	 */
	function Produksi_rph()
	{
		parent::__construct();
		$this->load->model('stok_model', '', TRUE);
		$this->load->model('auth_model', '', TRUE);
		$this->load->model('Home_model', '', TRUE);
		$this->load->model('rph_model', '', TRUE);
		$this->load->model('sapi_model', '', TRUE);
		$this->load->model('stok_model', '', TRUE);
		$this->load->model('produksi_rph_model', '', TRUE);
	}
	/**
	 * Iid_userialisasi variabel untuk $title(untuk id element <body>), dan */
	var $title = 'Produksi Rumah Potong Hewan';
	
	/**
	 * Memeriksa user state,
	 * jika tidak akan meredirect ke halaman login
	 */	
	function index()
	{
		//cek nih user udah login apa blom, kalo blom lempar ke halaman login
		if ($this->session->userdata('login') == FALSE)	
		{
			redirect('auth');
		}
		$id_role = $this->session->userdata('id_role'); 
		$id_user = $this->session->userdata('id_user'); 
				
		$data['hak_akses0'] = $this->Home_model->get_hak_akses($id_role,0);
		$data['hak_akses1'] = $this->Home_model->get_hak_akses($id_role,1);
		$data['hak_akses2'] = $this->Home_model->get_hak_akses($id_role,2);
		
		$data['title'] = $this->title;
		$data['main_view'] = 'produksi_rph/produksi_rph';
		$data['nama_user'] =  $this->session->userdata('username');
		$all_jenis_sapi = $this->sapi_model->get_jenis_sapi();
		if($all_jenis_sapi){
			$data['jenis_sapi_count'] = $all_jenis_sapi->num_rows();
		} else{
			$data['jenis_sapi_count'] = 0;
		}
		
		
		// Load view
		$this->load->view('template', $data);
	}
	
	function show_rph_list(){
		$id_rph = $this->input->post('id_rph');
		$rph_res = $this->sapi_model->get_jenis_sapi($id_rph);
		if($rph_res == null)
			$data['all_jenis_sapi'] = null;
		else {
			
			$array = array();
			foreach($rph_res->result_array() as $row){
				$id_sapi = $row['ID_SAPI'];
				$row['JUMLAH_STOK'] = $this->stok_model->get_stok_titik_pasok($id_rph, $id_sapi, 2);
				$array[] = $row;
			}
			
			$data['all_jenis_sapi'] = $array;
		}
		
		$data['id'] = $this->input->post('id');
		
		$this->load->view('produksi_rph/rph_result_list', $data);
	}
	
	function insert_to_prod_rph(){
		$status = intval($this->input->post('status'));
		$id_rph = $this->input->post('id_titik_pasok');
		$id_sapi = $this->input->post('id_sapi');
		$current_stok = $this->stok_model->get_stok_titik_pasok($id_rph, $id_sapi, 2);
		$changed_stok = intval($this->input->post('jumlah_sapi'));
		$id_daging = $this->input->post('id_daging');
		$produksi_daging = $this->input->post('produksi_daging');
		
		if(($current_stok - $changed_stok) > 0){
		
			/* mengurangi tabel stok di rph */
			$old_stok = array(
				'ID_USER' => $this->session->userdata('id_user'),
				'ID_SAPI' => $id_sapi,
				'ID_TITIK_PASOK' => $id_rph,
				'JUMLAH' => (-1 * $changed_stok),
				'REVISI' => 2,
				'ALASAN_PERUBAHAN' => 6
			);
			
			$this->stok_model->insert_to_stok($old_stok);
			
			/* menambah produksi rph */
			$prod_arr = array(
				'ID_TITIK_PASOK' => $this->input->post('id_titik_pasok'),
				'ID_SAPI' => $this->input->post('id_sapi'),
				'ID_DAGING_SAPI' => $id_daging,
				'JUMLAH_SAPI' => $this->input->post('jumlah_sapi'),
				'PRODUKSI_DAGING' => $produksi_daging,
				'JENIS_KELAMIN' => $this->input->post('jenis_kelamin'),
				'KATEGORI_UMUR' => $this->input->post('kategori_umur'),
				'KOMENTAR' => $this->input->post('komentar'),
				'REVISI' => $status
			);
			
			if($status == 3){
				$prod_arr['ALASAN_DITOLAK'] = $this->input->post('alasan_ditolak');
			}//var_dump($arr);
			
			$this->produksi_rph_model->insert_produksi_model($prod_arr);
			
			/* menambah stok daging di rph */
			
			$new_stok = array(
				'ID_USER' => $this->session->userdata('id_user'),
				'ID_SAPI' => $id_daging,
				'ID_TITIK_PASOK' => $id_rph,
				'JUMLAH' => $produksi_daging,
				'REVISI' => 2,
				'ALASAN_PERUBAHAN' => 7
			);
			
			$this->stok_model->insert_to_stok($new_stok);
			
		} else {
			echo '
				<div class="alert alert-error">
					<strong>Peringatan!</strong> Terjadi kesalahan pada jumlah stok.
				</div>';
		}
		
		
	}
	
	function auto_complete_daging(){
		// Cleaning up the term
		$term = trim(strip_tags($_GET['term']));
			 
		// Rudimentary search
		$matches = array();
		$daging_res = $this->sapi_model->get_daging_sapi();
		foreach($daging_res->result_array() as $sd){
			if(stripos($sd['NAMA'], $term) !== false){
				// Add the necessary "value" and "label" fields and append to result set
				$sd['value'] = $sd['NAMA'];
				$sd['label'] = "{$sd['NAMA']}";
				$matches[] = $sd;
			}
		}
		 
		// Truncate, encode and return the results
		$matches = array_slice($matches, 0, 10);
		echo json_encode($matches);
	}
	
	function auto_complete_rph(){
		// Cleaning up the term
		$term = trim(strip_tags($_GET['term']));
			 
		// Rudimentary search
		$matches = array();
		$rph_res = $this->rph_model->get_all_rph();
		foreach($rph_res->result_array() as $sd){
			if(stripos($sd['NAMA'], $term) !== false){
				// Add the necessary "value" and "label" fields and append to result set
				$sd['value'] = $sd['NAMA'];
				$sd['label'] = "{$sd['NAMA_KOTA']}, {$sd['NAMA']}";
				$matches[] = $sd;
			}
		}
		 
		// Truncate, encode and return the results
		$matches = array_slice($matches, 0, 10);
		echo json_encode($matches);
	}
	
}
	
	